home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.12 Dec 93 / Apple π / CAppPiPane.cp < prev    next >
Encoding:
Text File  |  1994-11-06  |  5.6 KB  |  228 lines  |  [TEXT/KAHL]

  1. /****
  2.  * CAppPiPane.c
  3.  *
  4.  *    Pane methods for a typical application.
  5.  *
  6.  *  Copyright © 1990 Symantec Corporation.  All rights reserved.
  7.  *
  8.  ****/
  9.  
  10. /**
  11.  *
  12.  *  Most applications will want a scrollable window, so this
  13.  *  class is based on the class CPanorama. All the methods here
  14.  *  would still apply to classes based directly on CPane.
  15.  *
  16.  **/
  17.  
  18. #include "CAppPiPane.h"
  19. #include <stdio.h>
  20.  
  21. #define Margin    5
  22.  
  23. void DrawWrapText (char *text, long count, short leftMargin, short rightMargin, short lineMargin, Boolean forceMargin);
  24.  
  25.  
  26. void CAppPiPane::IAppPiPane(CView *anEnclosure, CBureaucrat *aSupervisor,
  27.                             short aWidth, short aHeight,
  28.                             short aHEncl, short aVEncl,
  29.                             SizingOption aHSizing, SizingOption aVSizing)
  30. {
  31.     CPanorama::IPanorama(anEnclosure, aSupervisor, aWidth, aHeight,
  32.                             aHEncl, aVEncl, aHSizing, aVSizing);
  33.     
  34.     pi = nil;
  35.     time = 0;
  36. }
  37.  
  38.  
  39. char *CAppPiPane::GetContent (void)
  40. {
  41.     return (this->pi);
  42. }
  43.  
  44.  
  45. void CAppPiPane::SetContent (char *result, long ticks, long digits)
  46. {
  47.     char    *s;
  48.     
  49.     if (this->pi != nil)                //    Dispose existing ptr.
  50.         DisposPtr (this->pi);
  51.         
  52.     this->pi = result;                    //    Save passed in data.
  53.     this->time = ticks;
  54.     this->numDigits = digits;
  55.     
  56.     for (s = pi; s < pi + numDigits; s++)
  57.         *s += '0';                        //    Convert to numbers.
  58. }
  59.  
  60.  
  61. /***
  62.  * Draw
  63.  *
  64.  *    In this method, you draw whatever you need to display in
  65.  *    your pane. The area parameter gives the portion of the 
  66.  *    pane that needs to be redrawn. Area is in frame coordinates.
  67.  *
  68.  ***/
  69.  
  70. void CAppPiPane::Draw(Rect *area)
  71.  
  72. {
  73.     short    font;
  74.     short    charWidth;
  75.     Str255    title;
  76.     LongRect    ap;
  77.     char    *s;
  78.     long    output;
  79.     short    i, numChars;
  80.     
  81.     if (pi == nil)
  82.         return;
  83.         
  84.     GetFNum ("\pMonaco", &font);
  85.     TextFont (font);
  86.     TextSize (9);
  87.  
  88.     GetAperture (&ap);
  89.  
  90.     MoveTo (0, 20);
  91.     if (time == 0)
  92.         title[0] = sprintf ((char *) title + 1, "The value of π calculated to %ld digits:", numDigits);
  93.     else
  94.         title[0] = sprintf ((char *) title + 1, "The value of π calculated to %ld digits in %ld seconds:", numDigits, time / 60);
  95.     DrawWrapText ((char *) title + 1, title[0], Margin, ap.right - Margin, 0, true);
  96.     
  97.     MoveTo (0, 50);
  98.     DrawWrapText (pi, 1, Margin, ap.right - Margin, 0, true);
  99.     DrawWrapText (".", 1, Margin, ap.right - Margin, 0, false);
  100.     DrawWrapText (pi + 1, numDigits - 1, Margin, ap.right - Margin, 0, false);
  101.  
  102. }
  103.  
  104.  
  105. /***
  106.  * DoClick
  107.  *
  108.  *    The mouse went down in the pane.
  109.  *    In this method you do whatever is appropriate for your
  110.  *    application. HitPt is given in frame coordinates. The other
  111.  *    parameters, modiferKeys and when, are taken from the event
  112.  *    record.
  113.  *
  114.  *    If you want to implement mouse tracking, this is the method
  115.  *    to do it in. You need to create a subclass of CMouseTask and
  116.  *    pass it in a TrackMouse() message to the pane.
  117.  *
  118.  ***/ 
  119.  
  120. void CAppPiPane::DoClick(Point hitPt, short modifierKeys, long when)
  121.  
  122. {
  123.     /* what happens when the mouse goes down */
  124. }
  125.  
  126.  
  127. /***
  128.  * HitSamePart
  129.  *
  130.  *    Test whether pointA and pointB are in the same part.
  131.  *    "The same part" means different things for different applications.
  132.  *    In the default method, "the same part" means "in the same pane."
  133.  *    If you want a different behavior, override this method. For instance,
  134.  *    two points might be in the same part if they're within n pixels
  135.  *  of each other.
  136.  *
  137.  *    PointA and pointB are both in frame coordinates.
  138.  *
  139.  ***/
  140.  
  141. Boolean     CAppPiPane::HitSamePart(Point pointA, Point pointB)
  142.  
  143. {
  144.     return inherited::HitSamePart(pointA, pointB);
  145. }
  146.  
  147.  
  148. /***
  149.  * AdjustCursor
  150.  *
  151.  *    If you want the cursor to have a different shape in your pane,
  152.  *    do it in this method. If you want a different cursor for different
  153.  *    parts of the same pane, you'll need to change the mouseRgn like this:
  154.  *        1. Create a region for the "special area" of your pane.
  155.  *        2. Convert this region to global coordinates
  156.  *        3. Set the mouseRgn to the intersection of this region
  157.  *           and the original mouseRgn: SectRgn(mouseRgn, myRgn, mouseRgn);
  158.  *
  159.  *    The default method just sets the cursor to the arrow. If this is fine
  160.  *    for you, don't override this method.
  161.  *
  162.  ***/
  163.  
  164. void CAppPiPane::AdjustCursor(Point where, RgnHandle mouseRgn)
  165.  
  166. {
  167.     inherited::AdjustCursor(where, mouseRgn);
  168. }
  169.  
  170.  
  171. /***
  172.  * ScrollToSelection
  173.  *
  174.  *    If your pane is based on a Panorama (as this example is), you might
  175.  *    want to define what it means to have a selection and what it means to
  176.  *    scroll to that selection.
  177.  *
  178.  ***/
  179.  
  180. void CAppPiPane::ScrollToSelection(void)
  181.  
  182. {
  183.     /* scroll to the selection */
  184. }
  185.  
  186.  
  187. /*    Draw text that wraps from line to line.  Count characters from text.  The 
  188.     leftMargin and rightMargin are the point locations in the window where the
  189.     margins are.  The lineMargin is the line spacing, if zero, it's set from
  190.     the font.  If forceMargin is true, then  the routine starts from the left
  191.     margin. */
  192. void DrawWrapText (char *text, long count, short leftMargin, short rightMargin, short lineMargin, Boolean forceMargin)
  193. {
  194.     Point        pt;
  195.     FontInfo    fInfo;
  196.     long        lineStart, lineEnd, lineSize, trySize;
  197.     
  198.     if (forceMargin)  {
  199.         GetPen (&pt);                    //    Move to left margin.
  200.         Move (leftMargin - pt.h, 0);
  201.     }
  202.     
  203.     GetFontInfo (&fInfo);                //    Set line margin.
  204.     if (lineMargin == 0)
  205.         lineMargin = fInfo.ascent + fInfo.descent + fInfo.leading;
  206.     
  207.     //    Text is short, don't need to wrap it.
  208.     if (count < ((rightMargin - leftMargin) / fInfo.widMax))  {
  209.         DrawText (text, 0, count);
  210.         return;
  211.     }
  212.     
  213.     //    Need to wrap text.
  214.     GetPen (&pt);
  215.     lineSize = rightMargin - pt.h;
  216.     
  217.     for (lineStart = lineEnd = 0; lineEnd < count; )  {
  218.         for (trySize = 0; trySize <= lineSize && lineEnd < count; lineEnd++)  {
  219.             trySize += CharWidth (text[lineEnd]);
  220.         }
  221.         DrawText (text, lineStart, lineEnd - 1 - lineStart);
  222.         GetPen (&pt);
  223.         Move (leftMargin - pt.h, lineMargin);
  224.         lineSize = rightMargin - leftMargin;
  225.         lineStart = lineEnd;
  226.     }
  227. }
  228.